{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b468398f",
   "metadata": {},
   "source": [
    "----------------------------------------\n",
    "* 本周主要内容：图像识别（人脸识别）\n",
    "* 23春_API_人工智能与机器学习_week05    \n",
    "* 学习记录：薛欣怡\n",
    "\n",
    "-----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c5eb955",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "## 1.人脸识别概念和基础\n",
    "## 2.人脸识别应用\n",
    "## 3.人脸识别实践-face++\n",
    "> 1.API文档阅读规范            \n",
    "> 2.人脸检测（人脸分析）实践            \n",
    "> 3.输入/输出           \n",
    "> 4.代码错误找原因"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c5320d5",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1.人脸识别                                  \n",
    "> 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0c9828b",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6cbbfa96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679282208,badc3c49-de03-44eb-84e9-205c5814cdcf', 'time_used': 397, 'confidence': 82.699, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': '4a6aba364d27bc778cbcd3260f89cb9b', 'face_rectangle': {'top': 355, 'left': 331, 'width': 516, 'height': 516}}], 'faces2': [{'face_token': '2427fb03257f18fa3ab5795aa62459ac', 'face_rectangle': {'top': 183, 'left': 154, 'width': 220, 'height': 220}}], 'image_id1': 'h3k51nWsh1OTs3SYOfhr7A==', 'image_id2': 'VobBLK/PnFSfs9ZQrdbilA=='}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('image_file1.jpeg','rb')\n",
    "f_02 = open('image_file2.jpeg','rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params ={ 'api_key':'dJcWAkdQys5GdmIu0d6gDqNxejm2ZGfs',\n",
    "          'api_secret':'1XmrRreeb3uMsY7w3yjJs-jqdS14boeU',\n",
    "          \"image_base64_1\":img_01,\n",
    "          \"image_base64_2\":img_02,}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "637105b6",
   "metadata": {},
   "source": [
    "# 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14f1c941",
   "metadata": {},
   "source": [
    "## 2.1 人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c82af3e1",
   "metadata": {},
   "source": [
    "### 2.1.1 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9ddc88ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3450c770bc49f1ba22675dd15a24e767',\n",
       " 'time_used': 174,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1679284478,2fd1f0bd-8956-4569-ae2a-4c8d7c22030a',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params ={ 'api_key':'dJcWAkdQys5GdmIu0d6gDqNxejm2ZGfs',\n",
    "          'api_secret':'1XmrRreeb3uMsY7w3yjJs-jqdS14boeU',\n",
    "          'display_name':'广州南方学院1栋宿舍楼'\n",
    "         }\n",
    "response = requests.post(request_url, data=params, )\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74f110fe",
   "metadata": {},
   "source": [
    "### 2.1.2 检测人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0ef0033c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ab442025650d6775a5666eb71da496aa',\n",
       " 'tags': '',\n",
       " 'time_used': 100,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院1栋宿舍楼',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1679284519,48fa798c-324c-4ebc-b296-75d6caf01328',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params ={ 'api_key':'dJcWAkdQys5GdmIu0d6gDqNxejm2ZGfs',\n",
    "          'api_secret':'1XmrRreeb3uMsY7w3yjJs-jqdS14boeU',\n",
    "         'faceset_token':'ab442025650d6775a5666eb71da496aa',\n",
    "         \n",
    "         }\n",
    "response = requests.post(request_url, data=params, )\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0ebee77",
   "metadata": {},
   "source": [
    "### 2.1.3 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "708fafff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1687095721,458f7649-859b-47ea-b41c-62be382eea54', 'time_used': 102, 'faces': [{'face_token': '5b304984df5becd141f4a7ff2ca674f1', 'face_rectangle': {'top': 147, 'left': 210, 'width': 158, 'height': 158}}], 'image_id': 'APu5oYCxGnyn6roU7rhKGw==', 'face_num': 1}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测: 刘亦菲、蔡徐坤、高启强\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1栋/gqq.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"dJcWAkdQys5GdmIu0d6gDqNxejm2ZGfs\",\n",
    "    \"api_secret\":\"1XmrRreeb3uMsY7w3yjJs-jqdS14boeU\",\n",
    "    \"image_base64\":img,\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "41354db5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': 'ab442025650d6775a5666eb71da496aa', 'tags': '', 'time_used': 91, 'user_data': '', 'display_name': '广州南方学院1栋宿舍楼', 'face_tokens': [], 'face_count': 0, 'request_id': '1687095756,92e09a0f-0ef1-47f7-8c9d-567f81e07815', 'outer_id': ''}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params ={ 'api_key':'dJcWAkdQys5GdmIu0d6gDqNxejm2ZGfs',\n",
    "          'api_secret':'1XmrRreeb3uMsY7w3yjJs-jqdS14boeU',\n",
    "         'faceset_token':'ab442025650d6775a5666eb71da496aa',\n",
    "         'faceset_tokens':['',]\n",
    "         \n",
    "         }\n",
    "response = requests.post(request_url, data=params, )\n",
    "\n",
    "print(response.status_code)\n",
    "print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c58b8c59",
   "metadata": {},
   "source": [
    "### 2.1.4 检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e2a3a59",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
